package org.example.business;

import org.example.manage.MilkTeaManage;
import org.example.model.MilkTea;
import org.example.model.ShoppingCart;
import org.example.ui.customer.DispositionSettleDialog;
import org.example.ui.milk_order.MilkTeaItemPanel;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import java.util.List;

public class MilkTeaBusiness {
    public static JPanel kindPanel, teaPanel;
    public static JScrollPane scrollPane, kindScrollPanel;
    public static SpringLayout kindLayout, teaLayout;
    public static List<MilkTea> lists;
    public static List<String> kindList;
    private static JButton[] kinds;
    public static MilkTeaItemPanel[] teaItemPanels;
    public static void setMilkTeas(int type) {
        teaPanel.removeAll();
        AddToCartBusiness.itemMap.clear();
        if (type == 0) {
            lists = MilkTeaManage.listClassify(new MilkTeaManage().read());
            for (int i = 0; i < lists.size(); i++) {
                if (lists.get(i).condition.equals("下架")) {
                    lists.remove(i);
                }
            }
            teaItemPanels = new MilkTeaItemPanel[lists.size()];
            kindList = MilkTeaManage.getKinds(lists);
        }
        teaPanel.setPreferredSize(new Dimension(300,lists.size()*155 + kindList.size() * 20 + 20));
        for (int i = 0; i < lists.size(); i++) {
            if (type == 0) {
                teaItemPanels[i] = new MilkTeaItemPanel(lists.get(i), i);
                teaItemPanels[i].setBorder(null);
                AddToCartBusiness.itemMap.put(lists.get(i).milkName, teaItemPanels[i]);
            }
            if (AddToCartBusiness.map.containsKey(lists.get(i).milkName)) {
                teaItemPanels[i].setNumberLabel(AddToCartBusiness.map.get(lists.get(i).milkName));
            }
            teaPanel.add(teaItemPanels[i]);
            if (i == 0) {
                JLabel label = new JLabel(lists.getFirst().kind);
                label.setFont(new Font("", Font.BOLD, 20));
                label.setForeground(Color.BLACK);
                teaPanel.add(label);
                teaLayout.putConstraint(SpringLayout.WEST, label, 0, SpringLayout.WEST, teaPanel);
                teaLayout.putConstraint(SpringLayout.NORTH, label, 5, SpringLayout.NORTH, teaPanel);
                teaLayout.putConstraint(SpringLayout.NORTH, teaItemPanels[i], 5, SpringLayout.SOUTH, label);
                teaLayout.putConstraint(SpringLayout.SOUTH, teaItemPanels[i], 150, SpringLayout.SOUTH, label);
            } else {
                if (!lists.get(i).kind.equals(lists.get(i - 1).kind)) {
                    JLabel jLabel = new JLabel(lists.get(i).kind);
                    jLabel.setFont(new Font("", Font.BOLD, 20));
                    jLabel.setForeground(Color.BLACK);
                    teaPanel.add(jLabel);
                    teaPanel.add(teaItemPanels[i]);
                    teaLayout.putConstraint(SpringLayout.WEST, jLabel, 0, SpringLayout.WEST, teaPanel);
                    teaLayout.putConstraint(SpringLayout.NORTH, jLabel, 5, SpringLayout.SOUTH, teaItemPanels[i - 1]);
                    teaLayout.putConstraint(SpringLayout.NORTH, teaItemPanels[i], 5, SpringLayout.SOUTH, jLabel);
                    teaLayout.putConstraint(SpringLayout.SOUTH, teaItemPanels[i], 150, SpringLayout.SOUTH, jLabel);
                } else {
                    teaLayout.putConstraint(SpringLayout.NORTH, teaItemPanels[i], 0, SpringLayout.SOUTH, teaItemPanels[i - 1]);
                    teaLayout.putConstraint(SpringLayout.SOUTH, teaItemPanels[i], 150, SpringLayout.SOUTH, teaItemPanels[i - 1]);
                }
            }
            if (lists.get(i).inventory == 0) {
                teaItemPanels[i].setBackground(Color.CYAN);
            }
            int finalI = i;
            if (type == 0) {
                teaItemPanels[i].addMouseListener(new MouseAdapter() {
                    @Override
                    public void mouseClicked(MouseEvent e) {
                        if (lists.get(finalI).inventory != 0) {
                            new DispositionSettleDialog(lists.get(finalI)).setVisible(true);
                        } else JOptionPane.showMessageDialog(teaItemPanels[finalI], "该奶茶已售空");
                    }
                });
            }
            teaLayout.putConstraint(SpringLayout.WEST, teaItemPanels[i], 1, SpringLayout.WEST, teaPanel);
            teaLayout.putConstraint(SpringLayout.EAST, teaItemPanels[i], -1, SpringLayout.EAST, teaPanel);
        }
    }
    public static void setKinds() {
        kindPanel.removeAll();
        if (kindList.isEmpty()) {
            return;
        }
        kinds = new JButton[kindList.size()];
        kindPanel.setPreferredSize(new Dimension(100,kindList.size() * 60 + 20));
        for(int i = 0; i < kindList.size(); i++){
            kinds[i] = new JButton(kindList.get(i));
            kinds[i].setBorder(null);
            kinds[i].setBackground(Color.WHITE);
            kinds[i].setFont(new Font("华文行楷", Font.BOLD,25));
            int finalI = i;
            kinds[i].addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    kinds[finalI].setBackground(Color.CYAN);
                    for (int j = 0; j < kindList.size(); j++) {
                        if (j != finalI) {
                            kinds[j].setBackground(Color.WHITE);
                        }
                    }
                    scrollPane.getViewport().setViewPosition(new Point(0, MilkTeaManage.getKindI().get(finalI) * 151 + finalI * 10));
                }
            });
            kindPanel.add(kinds[i]);
            if (i == 0) {
                kindLayout.putConstraint(SpringLayout.NORTH, kinds[0],0, SpringLayout.NORTH, kindPanel);
                kindLayout.putConstraint(SpringLayout.SOUTH, kinds[0],60, SpringLayout.NORTH, kindPanel);
            }
            else {
                kindLayout.putConstraint(SpringLayout.NORTH, kinds[i],0, SpringLayout.SOUTH, kinds[i-1]);
                kindLayout.putConstraint(SpringLayout.SOUTH, kinds[i],60, SpringLayout.SOUTH, kinds[i-1]);
            }
            kindLayout.putConstraint(SpringLayout.WEST, kinds[i],1, SpringLayout.WEST, kindPanel);
            kindLayout.putConstraint(SpringLayout.EAST, kinds[i],-1, SpringLayout.EAST, kindPanel);
        }
        kinds[0].setBackground(Color.CYAN);
        kindScrollPanel.setVisible(false);
        kindScrollPanel.setVisible(true);
        kindPanel.setVisible(false);
        kindPanel.setVisible(true);
        scrollPane.addMouseWheelListener(new MouseWheelListener() {
            @Override
            public void mouseWheelMoved(MouseWheelEvent e) {
                double y = scrollPane.getViewport().getViewPosition().getY();
                int yt = 0;
                for (int i = 0; i < kindList.size(); i++) {
                    yt = MilkTeaManage.getKindI().get(i) * 151 + i * 20;
                    if (y - yt < 50.0 && y - yt > -50.0) {
                        kinds[i].setBackground(Color.CYAN);
                        for (int j = 0; j < kindList.size(); j++) {
                            if (j != i) {
                                kinds[j].setBackground(Color.WHITE);
                            }
                        }
                        break;
                    }
                }
            }
        });
    }
    public static boolean newMilkTea(JPanel panel, String price, String num, String milkName, String kind) {
        try {
            Double.parseDouble(price);
            Integer.parseInt(num);
        }catch (NumberFormatException e) {
            JOptionPane.showMessageDialog(panel, "价格或库存格式不正确");
            return false;
        }
        if (milkName.isEmpty()||kind.isEmpty()) {
            JOptionPane.showMessageDialog(panel, "奶茶名和类别不能为空");
            return false;
        }
        if (Double.parseDouble(price) < 0.0) {
            JOptionPane.showMessageDialog(panel, "奶茶价格不能为负数");
            return false;
        }
        if (Integer.parseInt(num) < 0) {
            JOptionPane.showMessageDialog(panel, "奶茶库存不能为负数");
            return false;
        }
        return true;
    }
}
